package com.letou.ls.ui;

import java.util.ArrayList;

import com.letou.ls.R;
import com.letou.ls.mode.AppConfigInfo;
import com.letou.ls.mode.LotteryTypeInfo;
import com.letou.ls.uitl.AppConfigManager;
import com.letou.ls.uitl.AppSettingPreferencesUtils;
import com.letou.ls.uitl.AppShowSettingType;
import com.letou.ls.uitl.Constant;
import com.letou.ls.uitl.PreferencesUtils;
import com.letou.ls.view.SelectButton;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 综合设置
 *
 * @author libin
 *
 *         2016年6月13日
 */
public class AppSettingActivity extends Activity implements OnClickListener {
    
    private ListAdapter mListAdapter;
    private int currentOrientation;

    private PopupWindow popupWindow;

    private RelativeLayout rl_caizhong_select;
    private View popupView;
    private ListView pop_listview;
    private TextView tv_content;

    private SelectButton sb_v;
    private SelectButton sb_h;

    private SelectButton sb_qihao;// 期号
    private SelectButton sb_qiansanzhi;// 前三直
    private SelectButton sb_kaijiang_num_type;// 开奖号码
    private SelectButton sb_kaijiang_num_plan_type;// 开奖号码分布

    private SelectButton sb_qiansan_plan_type;// 前三分布
    private SelectButton sb_hezhi_type;// 和值
    private SelectButton sb_kuadu_type;// 跨度
    private SelectButton sb_dahao_count_type;// 大号个数
    private SelectButton sb_danhao_count_type;// 单号个数

    private SelectButton sb_recommed_num_type;// 推荐号码
    private SelectButton sb_two_digits_type;// 两位
    private SelectButton sb_three_num_type;// 三位

    private SelectButton sb_missing_values_type;// 遗漏值
    private SelectButton sb_appear_num_type;// 出现次数
    private SelectButton sb_broken_line_type;// 走势折线


    private Button bt_save_prf;

    private String lotteryCode;

    public static Boolean sSaveConfig = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentOrientation =
                getIntent().getIntExtra("orientation", Configuration.ORIENTATION_LANDSCAPE);
        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setContentView(R.layout.activity_app_setting_landscape);
        } else {
            setContentView(R.layout.activity_app_setting_portrait);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        rl_caizhong_select = (RelativeLayout) findViewById(R.id.rl_caizhong_select);
        rl_caizhong_select.setOnClickListener(this);

        tv_content = (TextView) findViewById(R.id.tv_content);
        sb_v = (SelectButton) findViewById(R.id.sb_v);
        sb_v.setText("竖屏");
        sb_v.setOnClickListener(this);

        sb_h = (SelectButton) findViewById(R.id.sb_h);
        sb_h.setText("横屏");
        sb_h.setOnClickListener(this);

        sb_qihao = (SelectButton) findViewById(R.id.sb_qihao);
        sb_qihao.setText("期号");
        sb_qihao.setOnClickListener(this);

        sb_qiansanzhi = (SelectButton) findViewById(R.id.sb_qiansanzhi);
        sb_qiansanzhi.setText("前三直");
        sb_qiansanzhi.setSelectButton(true);
        sb_qiansanzhi.setOnClickListener(this);

        sb_kaijiang_num_type = (SelectButton) findViewById(R.id.sb_kaijiang_num_type);
        sb_kaijiang_num_type.setText("开奖号码");
        sb_kaijiang_num_type.setSelectButton(true);
        sb_kaijiang_num_type.setOnClickListener(this);

        sb_kaijiang_num_plan_type = (SelectButton) findViewById(R.id.sb_kaijiang_num_plan_type);
        sb_kaijiang_num_plan_type.setText("开奖号码分布");
        sb_kaijiang_num_plan_type.setSelectButton(false);
        sb_kaijiang_num_plan_type.setOnClickListener(this);

        sb_qiansan_plan_type = (SelectButton) findViewById(R.id.sb_qiansan_plan_type);
        sb_qiansan_plan_type.setText("前三分布");
        sb_qiansan_plan_type.setSelectButton(true);
        sb_qiansan_plan_type.setOnClickListener(this);

        sb_hezhi_type = (SelectButton) findViewById(R.id.sb_hezhi_type);
        sb_hezhi_type.setText("和值");
        sb_hezhi_type.setSelectButton(true);
        sb_hezhi_type.setOnClickListener(this);

        sb_kuadu_type = (SelectButton) findViewById(R.id.sb_kuadu_type);
        sb_kuadu_type.setText("跨度");
        sb_kuadu_type.setSelectButton(true);
        sb_kuadu_type.setOnClickListener(this);

        sb_dahao_count_type = (SelectButton) findViewById(R.id.sb_dahao_count_type);
        sb_dahao_count_type.setText("大号个数");
        sb_dahao_count_type.setSelectButton(true);
        sb_dahao_count_type.setOnClickListener(this);

        sb_danhao_count_type = (SelectButton) findViewById(R.id.sb_danhao_count_type);
        sb_danhao_count_type.setText("单号个数");
        sb_danhao_count_type.setSelectButton(true);
        sb_danhao_count_type.setOnClickListener(this);

        sb_recommed_num_type = (SelectButton) findViewById(R.id.sb_recommed_num_type);
        sb_recommed_num_type.setText("推荐号码");
        sb_recommed_num_type.setSelectButton(true);
        sb_recommed_num_type.setOnClickListener(this);

        sb_two_digits_type = (SelectButton) findViewById(R.id.sb_two_digits_type);
        sb_two_digits_type.setText("两位");
        sb_two_digits_type.setSelectButton(false);
        sb_two_digits_type.setOnClickListener(this);

        sb_three_num_type = (SelectButton) findViewById(R.id.sb_three_num_type);
        sb_three_num_type.setText("三位");
        sb_three_num_type.setSelectButton(true);
        sb_three_num_type.setOnClickListener(this);

        sb_missing_values_type = (SelectButton) findViewById(R.id.sb_missing_values_type);
        sb_missing_values_type.setText("遗漏值");
        sb_missing_values_type.setSelectButton(true);
        sb_missing_values_type.setOnClickListener(this);

        sb_appear_num_type = (SelectButton) findViewById(R.id.sb_appear_num_type);
        sb_appear_num_type.setText("出现次数");
        sb_appear_num_type.setSelectButton(true);
        sb_appear_num_type.setOnClickListener(this);

        sb_broken_line_type = (SelectButton) findViewById(R.id.sb_broken_line_type);
        sb_broken_line_type.setText("走势折线");
        sb_broken_line_type.setSelectButton(true);
        sb_broken_line_type.setOnClickListener(this);
        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            sb_h.setSelectButton(true);
            sb_v.setSelectButton(false);
        } else {
            sb_h.setSelectButton(false);
            sb_v.setSelectButton(true);
        }

        bt_save_prf = (Button) findViewById(R.id.bt_save_prf);
        bt_save_prf.setOnClickListener(this);

        // AppSettingPreferencesUtils.putString(this, Constant.PRE_TAG_CURRENT_LTTERY_CODE,
        // lotteryCode);
        if (Constant.lotteryTypeInfo != null) {
            lotteryCode = PreferencesUtils.getString(this, Constant.PRE_TAG_CURRENT_LTTERY_CODE);
            for (int i = 0; i < Constant.lotteryTypeInfo.size(); i++) {
                if (TextUtils.equals(lotteryCode, Constant.lotteryTypeInfo.get(i).getTypeCode())) {
                    tv_content.setTag(Constant.lotteryTypeInfo.get(i));
                    tv_content.setText(Constant.lotteryTypeInfo.get(i).getTypeName());
                }
            }
            initConfigs();
        } else {
            AppSettingActivity.this.finish();
        }

    }

    public void initConfigs() {
        
        AppConfigInfo info = AppConfigManager.getInstance(getApplicationContext())
                .readAppSettingConfigs(lotteryCode, sb_v.isSelectButton() ? 0 : 1);
        sb_v.setSelectButton(info.getScreenDirection() == 0);
        sb_h.setSelectButton(info.getScreenDirection() == 1);
        sb_qihao.setSelectButton(info.getQihao());
        sb_qiansanzhi.setSelectButton(info.getQisanzhi());
        sb_kaijiang_num_type.setSelectButton(info.getKaijianghaima());
        sb_kaijiang_num_plan_type.setSelectButton(info.getKaijianghaimafenbu());
        sb_qiansan_plan_type.setSelectButton(info.getQiansanfenbu());
        sb_hezhi_type.setSelectButton(info.getHezhi());
        sb_kuadu_type.setSelectButton(info.getKuadu());
        sb_dahao_count_type.setSelectButton(info.getDahaogeshu());
        sb_danhao_count_type.setSelectButton(info.getDanhaogeshu());
        sb_recommed_num_type.setSelectButton(info.getTuijianhaoma());
        sb_two_digits_type.setSelectButton(info.getErwei());
        sb_three_num_type.setSelectButton(info.getSanwei());
        sb_missing_values_type.setSelectButton(info.getYilouzhi());
        sb_appear_num_type.setSelectButton(info.getChuxiancishu());
        sb_broken_line_type.setSelectButton(info.getXianshizhexian());

        Log.e("cc", "initConfigs:" + info.toString());
    }

    /**
     * 列表显示逻辑 适配器.
     */
    public class ListAdapter extends BaseAdapter {
        
        private LayoutInflater mInflater;
        private ArrayList<LotteryTypeInfo> lotteryTypeInfo = new ArrayList<LotteryTypeInfo>();

        public ListAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public void updateData(ArrayList<LotteryTypeInfo> array) {
            if (array == null) {
                return;
            }
            this.lotteryTypeInfo = array;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return lotteryTypeInfo.size();
        }

        @Override
        public Object getItem(int position) {
            return lotteryTypeInfo.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.appsetting_listview_item, null);
            }
            LotteryTypeInfo lottery = (LotteryTypeInfo) getItem(position);

            TextView textImage =
                    (TextView) convertView.findViewById(R.id.scrrenswith_list_item_name);

            textImage.setTag(lottery.getTypeCode());
            textImage.setOnClickListener(AppSettingActivity.this);
            textImage.setText(lottery.getTypeName());

            return convertView;
        }
    }
    
    
    
    @Override
    public void onClick(View v) {
        Boolean isSelect;
        
        switch (v.getId()) {
            case R.id.bt_save_prf:
                LotteryTypeInfo info = (LotteryTypeInfo) tv_content.getTag();
                if (info != null) {
                    Toast.makeText(AppSettingActivity.this, "保存设置成功!", Toast.LENGTH_SHORT).show();
                    PreferencesUtils.putString(this, Constant.PRE_TAG_CURRENT_LTTERY_CODE,
                            info.getTypeCode());
                    int direction = sb_v.isSelectButton() ? 0 : 1;
                    PreferencesUtils.putInt(AppSettingActivity.this,
                            AppShowSettingType.SCREEN_DIRECTION_TYPE, direction);
                    String key = lotteryCode + direction;
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.QIANSANZHI_TYPE,
                            sb_qiansanzhi.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.KAIJIANG_NUM_TYPE,
                            sb_kaijiang_num_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.KAIJIANG_NUM_PLAN_TYPE,
                            sb_kaijiang_num_plan_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.QIANSAN_PLAN_TYPE,
                            sb_qiansan_plan_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this, key + AppShowSettingType.HEZHI_TYPE,
                            sb_hezhi_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this, key + AppShowSettingType.KUADU_TYPE,
                            sb_kuadu_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.DAHAO_COUNT_TYPE,
                            sb_dahao_count_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.DANHAO_COUNT_TYPE,
                            sb_danhao_count_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.RECOMMED_NUM_TYPE,
                            sb_recommed_num_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.TWO_DIGITS_TYPE,
                            sb_two_digits_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.THREE_NUM_TYPE,
                            sb_three_num_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.MISSING_VALUES_TYPE,
                            sb_missing_values_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.APPEAR_NUM_TYPE,
                            sb_appear_num_type.isSelectButton());
                    AppSettingPreferencesUtils.putBoolean(this,
                            key + AppShowSettingType.BROKEN_LINE_TYPE,
                            sb_broken_line_type.isSelectButton());
                    
                    AppConfigManager.getInstance(getApplicationContext())
                            .initAppsettingConfigs(info.getTypeCode());
                    sSaveConfig = true;
                    AppSettingActivity.this.finish();
                }
                break;
            case R.id.rl_caizhong_select:
                showWindow(v);
                break;
            case R.id.sb_v:
                isSelect = sb_v.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {// 黑龙江快乐十分 only竖版
                    sb_v.setSelectButton(true);
                    sb_h.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_v.setSelectButton(true);
                    sb_h.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    sb_v.setSelectButton(false);
                    sb_h.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    sb_v.setSelectButton(true);
                    sb_h.setSelectButton(false);
                }
                initConfigs();
                break;
            case R.id.sb_h:
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    sb_v.setSelectButton(true);
                    sb_h.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_v.setSelectButton(true);
                    sb_h.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    sb_v.setSelectButton(false);
                    sb_h.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    sb_v.setSelectButton(false);
                    sb_h.setSelectButton(true);
                }
                
                initConfigs();
                break;
            case R.id.sb_qihao:
                sb_qihao.setSelectButton(true);
                break;
            case R.id.sb_qiansanzhi:
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {// 黑龙江快乐十分 only竖版
                    // 互斥项为“开奖号码”
                    sb_qiansanzhi.setSelectButton(true);
                    sb_kaijiang_num_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {// 快乐麻将
                    sb_qiansanzhi.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {// 快乐十分麻将
                    // 互斥项为“开奖号码”
                    sb_qiansanzhi.setSelectButton(true);
                    sb_kaijiang_num_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    sb_qiansanzhi.setSelectButton(true);
                    sb_kaijiang_num_type.setSelectButton(false);
                }
                break;
            
            case R.id.sb_kaijiang_num_type:
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    sb_qiansanzhi.setSelectButton(false);
                    sb_kaijiang_num_type.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_qiansanzhi.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    // 互斥项为“开奖号码”
                    sb_qiansanzhi.setSelectButton(false);
                    sb_kaijiang_num_type.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    sb_qiansanzhi.setSelectButton(false);
                    sb_kaijiang_num_type.setSelectButton(true);
                }
                break;
            case R.id.sb_kaijiang_num_plan_type:
                isSelect = sb_kaijiang_num_plan_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    sb_kaijiang_num_plan_type.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_kaijiang_num_plan_type.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    sb_kaijiang_num_plan_type.setSelectButton(true);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (sb_v.isSelectButton()) {// 竖直
                        if (isSelect) {
                            if (sb_qiansan_plan_type.isSelectButton()) {
                                sb_kaijiang_num_plan_type.setSelectButton(false);
                            } else {
                                sb_broken_line_type.setSelectButton(false);
                            }
                        } else {
                            sb_kaijiang_num_plan_type.setSelectButton(true);
                            sb_broken_line_type.setSelectButton(false);
                            sb_broken_line_type.setSelectButton(false);
                        }
                    } else {
                        sb_kaijiang_num_plan_type.setSelectButton(true);
                    }
                }

                break;
            case R.id.sb_qiansan_plan_type:
                isSelect = sb_qiansan_plan_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    sb_qiansan_plan_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_qiansan_plan_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    sb_qiansan_plan_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (sb_v.isSelectButton()) {// 竖直
                        if (isSelect) {
                            if (sb_kaijiang_num_plan_type.isSelectButton()) {
                                sb_qiansan_plan_type.setSelectButton(false);
                            }
                        } else {
                            sb_qiansan_plan_type.setSelectButton(true);
                        }
                    } else {
                        sb_qiansan_plan_type.setSelectButton(true);
                    }
                }
                break;
            case R.id.sb_hezhi_type:
                isSelect = sb_hezhi_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    if (isSelect) {
                        sb_hezhi_type.setSelectButton(false);
                    } else {
                        sb_hezhi_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_hezhi_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    if (isSelect) {
                        sb_hezhi_type.setSelectButton(false);
                    } else {
                        sb_hezhi_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (isSelect) {
                        sb_hezhi_type.setSelectButton(false);
                    } else {
                        sb_hezhi_type.setSelectButton(true);
                    }
                }

                break;
            case R.id.sb_kuadu_type:
                isSelect = sb_kuadu_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    if (isSelect) {
                        sb_kuadu_type.setSelectButton(false);
                    } else {
                        sb_kuadu_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_kuadu_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    if (isSelect) {
                        sb_kuadu_type.setSelectButton(false);
                    } else {
                        sb_kuadu_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (isSelect) {
                        sb_kuadu_type.setSelectButton(false);
                    } else {
                        sb_kuadu_type.setSelectButton(true);
                    }
                }
                break;
            case R.id.sb_dahao_count_type:
                isSelect = sb_dahao_count_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    if (isSelect) {
                        sb_dahao_count_type.setSelectButton(false);
                    } else {
                        sb_dahao_count_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_dahao_count_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    if (isSelect) {
                        sb_dahao_count_type.setSelectButton(false);
                    } else {
                        sb_dahao_count_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (isSelect) {
                        sb_dahao_count_type.setSelectButton(false);
                    } else {
                        sb_dahao_count_type.setSelectButton(true);
                    }
                }
                break;
            case R.id.sb_danhao_count_type:
                isSelect = sb_danhao_count_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    if (isSelect) {
                        sb_danhao_count_type.setSelectButton(false);
                    } else {
                        sb_danhao_count_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_danhao_count_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    if (isSelect) {
                        sb_danhao_count_type.setSelectButton(false);
                    } else {
                        sb_danhao_count_type.setSelectButton(true);
                    }
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (isSelect) {
                        sb_danhao_count_type.setSelectButton(false);
                    } else {
                        sb_danhao_count_type.setSelectButton(true);
                    }
                }


                break;
            case R.id.sb_recommed_num_type:
                isSelect = sb_recommed_num_type.isSelectButton();
                if (isSelect) {
                    sb_recommed_num_type.setSelectButton(false);
                } else {
                    sb_recommed_num_type.setSelectButton(true);
                }
                break;
            case R.id.sb_two_digits_type:
                sb_two_digits_type.setSelectButton(true);
                sb_three_num_type.setSelectButton(false);
                break;
            
            case R.id.sb_three_num_type:
                sb_two_digits_type.setSelectButton(false);
                sb_three_num_type.setSelectButton(true);
                break;
            case R.id.sb_missing_values_type:
                isSelect = sb_missing_values_type.isSelectButton();
                if (isSelect) {
                    sb_missing_values_type.setSelectButton(false);
                } else {
                    sb_missing_values_type.setSelectButton(true);
                }
                break;
            case R.id.sb_appear_num_type:
                isSelect = sb_appear_num_type.isSelectButton();
                if (isSelect) {
                    sb_appear_num_type.setSelectButton(false);
                } else {
                    sb_appear_num_type.setSelectButton(true);
                }
                break;
            case R.id.sb_broken_line_type:
                isSelect = sb_broken_line_type.isSelectButton();
                if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_01)) {
                    sb_broken_line_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_02)) {
                    sb_broken_line_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_03)) {
                    sb_broken_line_type.setSelectButton(false);
                } else if (TextUtils.equals(lotteryCode, Constant.LOTTERY_CODE_04)) {
                    if (sb_v.isSelectButton()) {// 竖
                        if (!sb_kaijiang_num_plan_type.isSelectButton()) {
                            sb_broken_line_type.setSelectButton(true);
                        }
                    } else {
                        sb_broken_line_type.setSelectButton(true);
                    }
                }
                break;
            default:
                break;
        }
        // // // TODO Auto-generated method stub
        // String code = (String) arg0.getTag();
        //
        // Intent intent = new Intent();
        // if (!TextUtils.isEmpty(code)) intent.putExtra(Constant.PRE_TAG_CURRENT_LTTERY_CODE,
        // code);
        // setResult(RESULT_OK, intent);
        // finish();
    }

    private void showWindow(View v) {
        if (popupWindow == null) {
            LayoutInflater layoutInflater =
                    (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            popupView = layoutInflater.inflate(R.layout.popup_listview, null);
            pop_listview = (ListView) popupView.findViewById(R.id.pop_listview);

            mListAdapter = new ListAdapter(AppSettingActivity.this);
            pop_listview.setAdapter(mListAdapter);

            mListAdapter.updateData(Constant.lotteryTypeInfo);
            popupWindow = new PopupWindow(AppSettingActivity.this);
            popupWindow.setContentView(popupView);
        }
        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(rl_caizhong_select, 0, 10);

        pop_listview.setOnItemClickListener(new OnItemClickListener() {
            
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LotteryTypeInfo info = (LotteryTypeInfo) mListAdapter.getItem(position);
                popupWindow.dismiss();
                tv_content.setTag(info);
                lotteryCode = info.getTypeCode();
                tv_content.setText(info.getTypeName());
                initConfigs();
            }
        });
    }

}
